package com.chenxingnet.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Drivers;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.dto.vo.AccountingControlVo;
import com.chenxingnet.dto.vo.WaybillVo;
import com.chenxingnet.service.AccountingService;
import com.chenxingnet.service.DepartmentService;
import com.chenxingnet.service.DriversService;
import com.chenxingnet.service.WaybillsService;

@Controller
@RequestMapping("/accountlist")
public class AccountingController {
	
	@Resource
	private AccountingService accountingService;
	
	
	@Resource
	private DepartmentService  departmentService;
	
	
	@Resource
	private WaybillsService   waybillsService;
	
	@Resource
	private DriversService    driversService;
	
	//小写的mm表示的是分钟HH:mm:ss
		private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 会计查询页面--分页
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
    @RequestMapping("/selectAccountList")
	public String selectAccountList(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
    	//权限跳转判断--无需分页
    	try {
    		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
    		String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if(deptid == 2 && (!rolename.equals("管理员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "aaa_city";
			}
			if(deptid == 1 && (!rolename.equals("管理员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "city";
			}
		} catch (Exception e) {
		}
    	
		List<String>   drivers=new  ArrayList<String>();//组织返回驾驶员列表信息
		List<Department> deps=  departmentService.showDept();
		List<WaybillVo> wayList =    waybillsService.selectAll();
		for(int  i=0;i<wayList.size();i++){
			drivers.add(wayList.get(i).getTransport());
			
		}
	         Set set = new  HashSet(); //驾驶员去重复
	         List newList = new  ArrayList(); 
	         for (String cd:drivers) {
	            if(set.add(cd)){
	                newList.add(cd);
	            }
	        }
	         
	       //车架号去重
	         List<WaybillVo> wayListRes = removeVe(wayList);
		
		model.addAttribute("deps", deps);//返回搜索项目
		model.addAttribute("newList", newList);
		model.addAttribute("wayListRes", wayListRes);
		
		//获取搜索条件的所有字段
		String deptname =  request.getParameter("deptname");
		if(deptname.equals("") || deptname == null || deptname == ""){
			deptname="选择";
		}
		//判断是不是乱码
		if(isMessyCode(deptname) == true){
			deptname = new String(request.getParameter("deptname").getBytes("ISO-8859-1"),"UTF-8");
		}
		
		String zhiling = request.getParameter("zhiling");
		if( zhiling.equals("") || zhiling == null ||zhiling == ""){
			zhiling="选择";
		}
		//判断是不是乱码
		if(isMessyCode(zhiling) == true){
			zhiling = new String(request.getParameter("zhiling").getBytes("ISO-8859-1"),"UTF-8");
		}
		
		String chexing = request.getParameter("chexing");
		if( chexing.equals("") || chexing == null ||chexing == ""){
			chexing="选择";
		}
		//判断是不是乱码
		if(isMessyCode(chexing) == true){
			chexing = new String(request.getParameter("chexing").getBytes("ISO-8859-1"),"UTF-8");
		}
		
		String xingming = request.getParameter("xingming");
		if( xingming.equals("") || xingming == null || xingming == ""){
			xingming="选择";
		}
		//判断是不是乱码
		if(isMessyCode(xingming) == true){
			xingming = new String(request.getParameter("xingming").getBytes("ISO-8859-1"),"UTF-8");
		}		
		
		String jiesuan = request.getParameter("jiesuan");
		if(jiesuan.equals("") || jiesuan == null || jiesuan == ""){
			jiesuan="选择";
		}
		if(isMessyCode(jiesuan) == true){
			jiesuan = new String(request.getParameter("jiesuan").getBytes("ISO-8859-1"),"UTF-8");
		}
		
		String jiashiyuanjs = request.getParameter("jiashiyuanjs");
		if(jiashiyuanjs.equals("") || jiashiyuanjs == null || jiashiyuanjs == ""){
			jiashiyuanjs="选择";
		}
		if(isMessyCode(jiashiyuanjs) == true){
			jiashiyuanjs = new String(request.getParameter("jiashiyuanjs").getBytes("ISO-8859-1"),"UTF-8");
		}
		String fatime = request.getParameter("fatime1");
		if(fatime.equals("") || fatime == null || fatime == ""){
			fatime="选择";
		}
		if(isMessyCode(fatime) == true){
			fatime = new String(request.getParameter("fatime1").getBytes("ISO-8859-1"),"UTF-8");
		}
		String zhitime = request.getParameter("fatime2");
		if(zhitime.equals("") || zhitime == null || zhitime == ""){
			zhitime="选择";
		}
		if(isMessyCode(fatime) == true){
			zhitime = new String(request.getParameter("fatime2").getBytes("ISO-8859-1"),"UTF-8");
		}
		System.out.println("****************");
		List<AccountingControlVo> accoutlist = null;
		
		try {//根据条件搜索
			if(!deptname.equals("选择")|| !zhiling.equals("选择") || !chexing.equals("选择")||!xingming.equals("选择")||!jiesuan.equals("选择")||!jiashiyuanjs.equals("选择")||!fatime.equals("选择") || !zhitime.equals("选择")){
				 /*accoutlist = accountingService.selectAllBySerach(request, model,deptname,chexing,xingming,jiesuan,jiashiyuanjs);	*/
				Waybills waybills = new Waybills();
				if(!deptname.equals("选择")){
					if(deptname.equals("成都")){
						waybills.setDeptid(1);
					}
					if(deptname.equals("绵阳")){
						waybills.setDeptid(2);
					}
					model.addAttribute("deptname", deptname);
				}
				
				if(!zhiling.equals("选择")){
					waybills.setWinstruct(zhiling);
					model.addAttribute("zhiling", zhiling);
				}
				
				if(!chexing.equals("选择")){
					waybills.setWvehicle(chexing);
					model.addAttribute("chexing", chexing);
				}
				
				if(!xingming.equals("选择")){
					waybills.setWtransport(xingming);
					model.addAttribute("xingming", xingming);
				}
				
				if(!jiesuan.equals("选择")){//待处理
					if(jiesuan.equals("未结算")){
						waybills.setWinsurance(new BigDecimal(1));
					}
					if(jiesuan.equals("已结算")){
						waybills.setWinsurance(new BigDecimal(2));
					}
					model.addAttribute("jiesuan", jiesuan);
				}
				
				if(!jiashiyuanjs.equals("选择")){
					waybills.setWtimeout(jiashiyuanjs);
					model.addAttribute("jiashiyuanjs", jiashiyuanjs);
				}
				if(!fatime.equals("选择") && !zhitime.equals("选择")){
					waybills.setWremark(fatime);
					model.addAttribute("fatime", fatime);
					waybills.setWpicture(zhitime);
					model.addAttribute("zhitime", zhitime);
				}
				//按照搜索条件查询
				 accoutlist = accountingService.selectAllBySou(request, model, waybills);
				 System.out.println("accoutlist.size:"+accoutlist.size());
				 
				 
				 AccountingControlVo   heji2=new  AccountingControlVo();//合计信息组织返回
					heji2.setDeptname("合计");
					heji2.setShangxian(new BigDecimal(0));
					heji2.setQiangxian(new BigDecimal(0));
					heji2.setChangjia(new  BigDecimal(0));
					heji2.setJiashiyuan(new  BigDecimal(0));
					heji2.setLinpai(new BigDecimal(0));
					heji2.setLirun(new BigDecimal(0));
					for(int y=0;y<accoutlist.size();y++){
						heji2.setShangxian(heji2.getShangxian().add(accoutlist.get(y).getShangxian()));
						heji2.setQiangxian(heji2.getQiangxian().add(accoutlist.get(y).getQiangxian()));
						heji2.setChangjia(heji2.getChangjia().add(accoutlist.get(y).getChangjia()));
						heji2.setJiashiyuan(heji2.getJiashiyuan().add(accoutlist.get(y).getJiashiyuan()));
						heji2.setLinpai(heji2.getLinpai().add(accoutlist.get(y).getLinpai()));
						heji2.setLirun(heji2.getLirun().add(accoutlist.get(y).getLirun()));
					}
					accoutlist.add(heji2);
				 
					model.addAttribute("accoutlist", accoutlist);//数据返回***************
					return "account_list";
				 
			}
		} catch (Exception e) {/*e.printStackTrace();*/}
		
		accoutlist = accountingService.selectAll(request, model);//获取所需运单数据*****
		
		AccountingControlVo   heji2=new  AccountingControlVo();//合计信息组织返回
		heji2.setDeptname("合计");
		heji2.setShangxian(new BigDecimal(0));
		heji2.setQiangxian(new BigDecimal(0));
		heji2.setChangjia(new  BigDecimal(0));
		heji2.setJiashiyuan(new  BigDecimal(0));
		heji2.setLinpai(new BigDecimal(0));
		heji2.setLirun(new BigDecimal(0));
		for(int y=0;y<accoutlist.size();y++){
			heji2.setShangxian(heji2.getShangxian().add(accoutlist.get(y).getShangxian()));
			heji2.setQiangxian(heji2.getQiangxian().add(accoutlist.get(y).getQiangxian()));
			heji2.setChangjia(heji2.getChangjia().add(accoutlist.get(y).getChangjia()));
			heji2.setJiashiyuan(heji2.getJiashiyuan().add(accoutlist.get(y).getJiashiyuan()));
			heji2.setLinpai(heji2.getLinpai().add(accoutlist.get(y).getLinpai()));
			heji2.setLirun(heji2.getLirun().add(accoutlist.get(y).getLirun()));
		}
		accoutlist.add(heji2);
		
		model.addAttribute("accoutlist", accoutlist);//数据返回***************
		return "account_list";
	}
    /** 
     * 判断字符串是否是乱码 
     * 
     * @param strName 字符串 
     * @return 是否是乱码 
     */  
    public static boolean isMessyCode(String strName) {  
        Pattern p = Pattern.compile("\\s*|t*|r*|n*");  
        Matcher m = p.matcher(strName);  
        String after = m.replaceAll("");  
        String temp = after.replaceAll("\\p{P}", "");  
        char[] ch = temp.trim().toCharArray();  
        float chLength = ch.length;  
        float count = 0;  
        for (int i = 0; i < ch.length; i++) {  
            char c = ch[i];  
            if (!Character.isLetterOrDigit(c)) {  
                if (!isChinese(c)) {  
                    count = count + 1;  
                }  
            }  
        }   
        float result = count / chLength;  
        if (result > 0.4) {  
            return true;  
        } else {  
            return false;  
        }  
   
    } 
    /* 判断字符是否是中文 
     * 
     * @param c 字符 
     * @return 是否是中文 
     */ 
    private static boolean isChinese(char c) {
    	Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
            return true;  
        }  
		return false;
	}
	
    
    /**
	 * 车架号 去重
	 * @param cars
	 * @return
	 */
    List<WaybillVo> removeVe(List<WaybillVo> wayVo){
		for (int i = 0; i < wayVo.size()-1; i++) {
			for (int j = wayVo.size()-1;j>i; j--) {
				if(wayVo.get(j).getVehicle().equals(wayVo.get(i).getVehicle())){
					wayVo.remove(j);
				}
			}
		}
		return wayVo;
	}
    /**
	* @Title: /accountlist/mobile_selectAccountList
	*移动端会计查询
	 */
    @RequestMapping("/mobile_selectAccountList")
   	public void mobile_selectAccountList(HttpServletRequest request,Model model,HttpServletResponse response){
    	
    	try {
			
    		response.setHeader("Access-Control-Allow-Origin", "*");
    		response.setContentType("application/json;charset=utf-8");
    		JSONObject returnJson = new JSONObject();
    		returnJson.put("falg","true");
           //权限跳转判断--无需分页
           	try {
           		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
           		String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
       			if(deptid != null){
       				returnJson.put("deptid", deptid);
       			}
       			if(deptid == 2 && (!rolename.equals("管理员"))){
       				returnJson.put("result", "对不起，您无此操作权限");
    				returnJson.put("deptid", deptid);
       			}
       			if(deptid == 1 && (!rolename.equals("管理员"))){
       				returnJson.put("result", "对不起，您无此操作权限");
    				returnJson.put("deptid", deptid);
       			}
       		} catch (Exception e) {
       		}
    		
    		List<String>   drivers=new  ArrayList<String>();//组织返回驾驶员列表信息
    		List<Department> deps=  departmentService.showDept();
    		List<WaybillVo> wayList =    waybillsService.selectAll();
    		for(int  i=0;i<wayList.size();i++){
    			drivers.add(wayList.get(i).getTransport());
    			
    		}
    	         Set set = new  HashSet(); //驾驶员去重复
    	         List newList = new  ArrayList(); 
    	         for (String cd:drivers) {
    	            if(set.add(cd)){
    	                newList.add(cd);
    	            }
    	        }
    	         
    	       //车架号去重
    	         List<WaybillVo> wayListRes = removeVe(wayList);
    		
    		returnJson.put("deps", deps);//返回搜索项目
       		returnJson.put("newList", newList);
       		returnJson.put("wayListRes", wayListRes);
    		
    		String deptname = request.getParameter("deptname");//获取搜索条件的所有字段
    		returnJson.put("deptname", deptname);
    		String chexing = request.getParameter("chexing");
    		returnJson.put("chexing", chexing);
    		String xingming = request.getParameter("xingming");
    		returnJson.put("xingming", xingming);
    		String jiesuan = request.getParameter("jiesuan");
    		returnJson.put("jiesuan", jiesuan);
    		String jiashiyuanjs = request.getParameter("jiashiyuanjs");
    		returnJson.put("jiashiyuanjs", jiashiyuanjs);
    		
    		List<AccountingControlVo> accoutlist = accountingService.selectAll(request, model);//获取所需运单数据*****
    		
    		try {//根据条件搜索
    			if(!deptname.equals("选择")||!chexing.equals("选择")||!xingming.equals("选择")||!jiesuan.equals("选择")||!jiashiyuanjs.equals("选择")){
    				 accoutlist = accountingService.selectAllBySerach(request, model,deptname,chexing,xingming,jiesuan,jiashiyuanjs);	
    			}
    		} catch (Exception e) {/*e.printStackTrace();*/}
    		returnJson.put("accoutlist", accoutlist);//数据返回***************
    		
    		AccountingControlVo   heji2=new  AccountingControlVo();//合计信息组织返回
    		heji2.setDeptname("合计");
    		heji2.setShangxian(new BigDecimal(0));
    		heji2.setQiangxian(new BigDecimal(0));
    		heji2.setChangjia(new  BigDecimal(0));
    		heji2.setJiashiyuan(new  BigDecimal(0));
    		heji2.setLinpai(new BigDecimal(0));
    		heji2.setLirun(new BigDecimal(0));
    		for(int y=0;y<accoutlist.size();y++){
    			heji2.setShangxian(heji2.getShangxian().add(accoutlist.get(y).getShangxian()));
    			heji2.setQiangxian(heji2.getQiangxian().add(accoutlist.get(y).getQiangxian()));
    			heji2.setChangjia(heji2.getChangjia().add(accoutlist.get(y).getChangjia()));
    			heji2.setJiashiyuan(heji2.getJiashiyuan().add(accoutlist.get(y).getJiashiyuan()));
    			heji2.setLinpai(heji2.getLinpai().add(accoutlist.get(y).getLinpai()));
    			heji2.setLirun(heji2.getLirun().add(accoutlist.get(y).getLirun()));
    		}
    		accoutlist.add(heji2);
    		
    		returnJson.put("page", request.getAttribute("mobilePage"));
			
			response.getWriter().print(returnJson.toString());
    		
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
   	}
    
    
    
    /**
	 * 会计查询单个页面
	 * @param model
	 * @return
	 */
    @RequestMapping("/selectAccountSingle")
	public String selectAccountSingle(HttpServletRequest request,Model model){
    	String wayidSingle = request.getParameter("wayidSingle");
    	String pageNow = request.getParameter("pageNow");
    	String[] args=request.getParameterValues("checkbox");
    	if(wayidSingle != null||args!=null){
    		if(args==null){
    			int sum= waybillsService.updateSetSongda(wayidSingle);
    		}else{
    			for (String str : args) {
    				wayidSingle = str;
    				int sum= waybillsService.updateSetSongda(wayidSingle);
				}
    		}
    		try {
        		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
        		System.out.println(deptid);
        		String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
        		if(deptid != null){
        			model.addAttribute("deptid", deptid);
        		}
        		//if(deptid == 2 && (!rolename.equals("管理员"))){
        		if(deptid == 2){
        			model.addAttribute("result", "运单已送达，请联系会计进行结算");
        			model.addAttribute("deptid", deptid);
        			//return "aaa_city";
        			return "redirect:/waybills/showWaybill?pageNow="+pageNow+"&deptid="+deptid;
        		}
        		//if(deptid == 1 && (!rolename.equals("管理员"))){
        		if(deptid == 1){
        			model.addAttribute("result", "运单已送达，请联系会计进行结算");
        			model.addAttribute("deptid", deptid);
        			//return "city";
        			return "redirect:/waybills/showWaybill?pageNow="+pageNow+"&deptid="+deptid;
        		}
        	} catch (Exception e) {
        		
        	}
    	}else{
    		
    		try {
    			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
    			String rolename=request.getParameter("rolename");
    			if(deptid != null){
    				model.addAttribute("deptid", deptid);
    			}
    			if(deptid == 2 && (!rolename.equals("管理员"))){
    				model.addAttribute("result", "对不起，您无此操作权限");
    				model.addAttribute("deptid", deptid);
    				return "aaa_city";
    			}
    			if(deptid == 1 && (!rolename.equals("管理员"))){
    				model.addAttribute("result", "对不起，您无此操作权限");
    				model.addAttribute("deptid", deptid);
    				return "city";
    			}
    		} catch (Exception e) {
    			
    		}
    	}
    	
    	
    	
		List<String>   drivers=new  ArrayList<String>();//组织返回驾驶员列表信息
	
		List<Department> deps=  departmentService.showDept();
		List<WaybillVo> wayList =    waybillsService.selectAll();
		List<Drivers>    drs=   driversService.selectAll();//获取司机信息
		
		for(int  i=0;i<wayList.size();i++){
			drivers.add(wayList.get(i).getTransport());
			
		}
	         Set set = new  HashSet(); //驾驶员去重复
	         List newList = new  ArrayList(); 
	         for (String cd:drivers) {
	            if(set.add(cd)){
	                newList.add(cd);
	            }
	        }
		
	         //车架号去重
	         List<WaybillVo> wayListRes = removeVe(wayList);
	         
		model.addAttribute("deps", deps);
		model.addAttribute("newList", newList);
		model.addAttribute("wayListRes", wayListRes);
		String deptname = request.getParameter("deptname");//获取搜索条件的所有字段
		model.addAttribute("deptname", deptname);
		String chexing = request.getParameter("chexing");
		model.addAttribute("chexing", chexing);
		String xingming = request.getParameter("xingming");
		model.addAttribute("xingming", xingming);
		String jiesuan = request.getParameter("jiesuan");
		model.addAttribute("jiesuan", jiesuan);
		
		
		List<AccountingControlVo> accoutlist = accountingService.selectAllSingle(Integer.parseInt(wayidSingle));
		try {
			if(!deptname.equals("")){
				//accoutlist = accountingService.selectAllBySerach(deptname,chexing,xingming,jiesuan);
			}
		} catch (Exception e) {}
		
		model.addAttribute("accoutlist", accoutlist);
		AccountingControlVo   heji2=new  AccountingControlVo();
		heji2.setDeptname("合计");
		/*heji2.setBaoxian(new  BigDecimal(0));*/
		heji2.setShangxian(new BigDecimal(0));
		heji2.setQiangxian(new BigDecimal(0));
		heji2.setChangjia(new  BigDecimal(0));
		heji2.setJiashiyuan(new  BigDecimal(0));
		heji2.setLinpai(new BigDecimal(0));
		heji2.setLirun(new BigDecimal(0));
		for(int y=0;y<accoutlist.size();y++){
			/*heji2.setBaoxian(heji2.getBaoxian().add(accoutlist.get(y).getBaoxian()));*/
			heji2.setShangxian(heji2.getShangxian().add(accoutlist.get(y).getShangxian()));
			heji2.setQiangxian(heji2.getQiangxian().add(accoutlist.get(y).getQiangxian()));
			heji2.setChangjia(heji2.getChangjia().add(accoutlist.get(y).getChangjia()));
			heji2.setJiashiyuan(heji2.getJiashiyuan().add(accoutlist.get(y).getJiashiyuan()));
			heji2.setLinpai(heji2.getLinpai().add(accoutlist.get(y).getLinpai()));
			heji2.setLirun(heji2.getLirun().add(accoutlist.get(y).getLirun()));
		}
		accoutlist.add(heji2);
		return "account_list";
	}
    
	
	 /** 
     * 使用Ajax异步上传图片 
     *  
     * @param pic 封装图片对象 
     * @param request 
     * @param response 
     * @throws IOException  
     * @throws IllegalStateException  
     */  
	 @RequestMapping("/uploadPic")    
    public void uploadPic(MultipartFile pic, HttpServletRequest request, HttpServletResponse response,Model model) throws IllegalStateException, IOException {  
  
		 try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					model.addAttribute("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
    	System.out.println("xxxx");
    	System.out.println(pic);
    	
    	String deptname = request.getParameter("file");
    	System.out.println(deptname);
        try {  
            // 获取图片原始文件名  
            String originalFilename = pic.getOriginalFilename();  
            System.out.println(originalFilename);  
          
            // 文件名使用当前时间  
            String name = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());  
          
            // 获取上传图片的扩展名(jpg/png/...)  
            String extension = FilenameUtils.getExtension(originalFilename);  
              
            // 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）  
            String path = "/upload/" + name + "." + extension;  
  
            // 图片上传的绝对路径  
            String url = request.getSession().getServletContext().getRealPath("") + path;  
          
                File dir = new File(url);  
            if(!dir.exists()) {  
            dir.mkdirs();  
            }  
              
            // 上传图片  
            pic.transferTo(new File(url));  
          
            // 将相对路径写回（json格式）  
            JSONObject jsonObject = new JSONObject();  
            // 将图片上传到本地  
            jsonObject.put("path", path);  
          
            // 设置响应数据的类型json  
            response.setContentType("application/json; charset=utf-8");  
            // 写回  
            response.getWriter().write(jsonObject.toString());  
  
        } catch (Exception e) {  
            throw new RuntimeException("服务器繁忙，上传图片失败");  
        }  
    }  
      
	 
	
	

}
